<?php

namespace app\test\controller;


use think\Controller;
use think\Db;
use think\facade\View;
use think\Request;

class Two extends Controller
{

    public function index()
    {
        //
    }
//增加
    public function create()
    {
        if (\request()->isPost()){
            $arr=\request()->post();
            //文件上传
            $file=\request()->file('img');
            $info=$file->move('./uploads');
            $arr['img']='/uploads/'.$info->getSavename();
            $user=new \app\test\model\Two();
            $res=$user->table('two')->insert($arr);
            if ($res){
                $this->success('添加成功','show');
            }else{
                $this->error('添加失败','create');
            }
        }
        return View::fetch('create');
    }
    //展示
    public function show(Request $request)
    {
        $keywordone=$request->param('keywordone');
        $keywordtwo=$request->param('keywordtwo');
        if ($keywordone||$keywordtwo){
            $user=new \app\test\model\Two();
//            $data=$user->table('two')->where([['name','like','%'.$keywordone.'%'],['class','like','%'.$keywordtwo.'%']])->paginate(
//                [
//                    'list_rows'=>2,
//                    'query'=>$request->param()
//                ]);
            $data=$user->table('two')->where(['name'=>$keywordone,'class'=>$keywordtwo])->paginate(
                [
                    'list_rows'=>2,
                    'query'=>$request->param()
                ]);
        }else{
            $user=new \app\test\model\Two();
            $data=$user->table('two')->paginate(2);
        }

       return View::fetch('show',['data'=>$data]);
    }

    public function info($id)
    {
        $id=\request()->param('id');
        $user=new \app\test\model\Two();
        $res=$user->table('two')->where('id',$id)->find($id);
       return View::fetch('info',['data'=>$res]);
    }
    public function update()
    {
        $id=\request()->param('id');
        $arr=\request()->post();
        //文件上传
        $file=\request()->file('img');
        $info=$file->move('./uploads');
        $arr['img']='/uploads/'.$info->getSavename();
        $user=new \app\test\model\Two();
        $res=$user->table('two')->where('id',$id)->update($arr);
        if ($res){
            $this->success('修改成功','show');
        }else{
            $this->error('修改失败','info');
        }

    }

    public function del($id)
    {
        $id=\request()->param('id');
        $user=new \app\test\model\Two();
        $res=$user->table('two')->where('id',$id)->delete($id);
        if ($res){
            return json(['code'=>200,'msg'=>'删除成功','data'=>$res]);
        }else{
            return json(['code'=>400,'msg'=>'删除失败','data'=>null]);
        }

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
